package com.heatingcharge.service.base.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.heatingcharge.common.exception.BaseException;
import com.heatingcharge.common.util.BeanValidator;
import com.heatingcharge.common.util.ResponseCodeEnum;
import com.heatingcharge.dto.ChargePriceDTO;
import com.heatingcharge.entity.base.ChargePrice;
import com.heatingcharge.mapper.base.ChargePriceMapper;
import com.heatingcharge.param.ChargePriceParam;
import com.heatingcharge.security.JwtUser;
import com.heatingcharge.security.util.JwtUtil;
import com.heatingcharge.security.util.SecurityUtil;
import com.heatingcharge.service.base.IChargePriceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author devin.jiang
 * @since 2019-10-15
 */
@Service
public class ChargePriceServiceImpl extends ServiceImpl<ChargePriceMapper, ChargePrice> implements IChargePriceService {

    @Override
    public Integer save(ChargePriceParam chargePriceParam) {
        //校验字段合法性
        BeanValidator.check(chargePriceParam);
        //检验年度是否已经存在
        if (checkExist(chargePriceParam.getHeatYearId(),null)){
            //年度已经存在
            throw new BaseException(ResponseCodeEnum.CHARGEPRICE_EXIST.getMessage(),ResponseCodeEnum.CHARGEPRICE_EXIST.getCode());
        }
        JwtUser user = SecurityUtil.getUser();
        ChargePrice chargePrice = ChargePrice.builder()
                .heatYearId(chargePriceParam.getHeatYearId())
                .civilPrice(chargePriceParam.getCivilPrice())
                .comlPrice(chargePriceParam.getComlPrice())
                .pubuildPrice(chargePriceParam.getPubuildPrice())
                .lowincoPrice(chargePriceParam.getLowincoPrice())
                .createdBy(user.getRealName())
                .build();
        return baseMapper.insert(chargePrice);
    }

    private boolean checkExist(Integer heatYearId, Integer id) {
        QueryWrapper<ChargePrice> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("heat_year_id",heatYearId);
        if (id!=null){
            queryWrapper.ne("id",id);
        }
        return baseMapper.selectOne(queryWrapper) !=null;
    }

    @Override
    public Integer update(ChargePriceParam chargePriceParam) {
        //校验字段合法性
        BeanValidator.check(chargePriceParam);
        //检验年度是否已经存在
        if (checkExist(chargePriceParam.getHeatYearId(), chargePriceParam.getId())){
            //年度已经存在
            throw new BaseException(ResponseCodeEnum.CHARGEPRICE_EXIST.getMessage(),ResponseCodeEnum.CHARGEPRICE_EXIST.getCode());
        }
        ChargePrice before=baseMapper.selectById(chargePriceParam.getId());
        //待更新的不存在
        if (ObjectUtil.isNull(before)){
            throw new BaseException(ResponseCodeEnum.CHARGEPRICE_NON_EXIST.getMessage(),ResponseCodeEnum.CHARGEPRICE_NON_EXIST.getCode());
        }
        ChargePrice after =new ChargePrice();
        JwtUser user = SecurityUtil.getUser();
        BeanUtils.copyProperties(chargePriceParam,after);
        after.setUpdatedBy(user.getRealName());
        return baseMapper.updateById(after);
    }

    @Override
    public Integer delete(ChargePriceParam chargePriceParam) {
        return null;
    }

    @Override
    public List<ChargePriceDTO> getChargePrice(ChargePriceDTO chargePriceDTO) {
        return baseMapper.selectChargePrice(chargePriceDTO);
    }

    /**
     * 获取指定供暖季的价格信息
     * @param heatYearId
     * @return
     */
    @Override
    public ChargePrice getChargePriceByHeatYearId(Integer heatYearId) {
        QueryWrapper<ChargePrice> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("heat_year_id",heatYearId);
        queryWrapper.eq("del_flag",0);
        return baseMapper.selectOne(queryWrapper);
    }
}
